﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace TheSnake
{
    public struct ScoresData
    {
        public static List<ScoresData> scoreLists = new List<ScoresData>();
        private int points;

        public int Points
        {
            get { return points; }
            set { points = value; }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public ScoresData(string nameLine, int score)
        {
            points = score;
            name = nameLine;
        }
    }
    public static class SaveScore
    {
        public enum HowToSort { ascending ,descending }
        public static void SaveScores(String name, int scores,bool overwrite)
        {
            StreamWriter writer = new StreamWriter("scores.txt", overwrite);
            using (writer)
            {
                writer.WriteLine("{0} {1}", name, scores);
            }
            writer.Close();
        }
        public static void Sort(string sortBy, string sortDirection,string name)
        {
            if (sortBy == "Points")
            {
                var linq = (from ScoresData points in ScoresData.scoreLists
                            orderby points.Points descending, points.Name descending 
                            select points);
                Console.WriteLine("\n Sorted List");
                int height = 1000,width=300;
                Console.SetBufferSize(width,height);
                foreach (ScoresData data in linq)
                {
                    if (data.Name.Equals(name))
                    {
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                        Console.WriteLine("{0},{1}", data.Name, data.Points);
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.Black;
                        Console.WriteLine("{0},{1}", data.Name, data.Points);
                    }
                }
            }
        }
        public static bool IsExists(string name) 
        {
            bool isExist = false;
            List<ScoresData> players = new List<ScoresData>();
            StreamReader searcher = new StreamReader("scores.txt");
            using (searcher)
            {
                string[] parts;
                string line = searcher.ReadLine();
                while (line != null)
                {
                    line = searcher.ReadLine();
                    if (line != null)
                    {
                        parts = line.Split(' ');
                        ScoresData newData = new ScoresData(parts[0], Int32.Parse(parts[1]));
                        players.Add(newData);
                    }
                }
            }
            searcher.Close();
            foreach (ScoresData data in players)
            {
                if (data.Name.Equals(name))
                {
                    isExist = true;
                    break;
                }
            }
            return isExist;
        }
        public static void ReadScores(string name)
        {
            StreamReader reader = new StreamReader("scores.txt");
            using (reader)
            {
                string[] parts;
                string line = reader.ReadLine();
                while (line != null)
                {
                   line = reader.ReadLine();
                   if (line != null)
                   {
                       parts = line.Split(' ');
                       ScoresData newData = new ScoresData(parts[0], Int32.Parse(parts[1]));
                       ScoresData.scoreLists.Add(newData);
                   }
                }
            }
            Sort("Points", "DESC", name);
            reader.Close();
        }
    }
}

